import Vue from "vue";
import Vuex from "vuex";
import router from "@/router"
import { Message } from 'element-ui';
import { login } from '@/api/login'
import { getItem, setItem } from '@/utils/storage'
import { getGoodsCate } from '@/api/goods'
Vue.use(Vuex);
const SHOP_USER = 'SHOP_USER'
export default new Vuex.Store({
  state: {
    userInfo: getItem(SHOP_USER) || {},
    cateList: []  //商品分流数据
  },
  mutations: {
    setUserInfo(state, data) {
      state.userInfo = data
      setItem(SHOP_USER, data)
    },
    setCate(state, data) {
      state.cateList = data
    }
  },
  actions: {
    async login(content, data) {
      let res = await login(data)
      const vm = this
      if (res.meta.status !== 200) {
        // 请求错误处理'

        return Message.error(res.meta.msg)
      }
      console.log(res);
      content.commit('setUserInfo', res.data)
      // console.log(vm);
      console.log(router.currentRoute);
      // console.log(vm.$route);
      router.push("/");
    },
    async getCate({ commit }) {
      const { data } = await getGoodsCate()
      commit('setCate', data)
      console.log(data);
    }
  },
  getters: {
    token: state => state.userInfo.token,
    username: state => state.userInfo.username
  },
  modules: {},
});
